//
//  HSDrawerController.swift
//  SchoolDemo
//
//  Created by rimi on 16/9/27.
//  Copyright © 2016年 YQ. All rights reserved.
//

import UIKit

class HSDrawerController: UIViewController {
    //左边视图
    let leftViewVc = UIStoryboard(name: "Main", bundle: nil).instantiateViewController(withIdentifier: "HSLeftViewController")

    //添加标签控制器
    let tabBarVc = HStabBarController()
    
    
    //动画时长
    let animationTime: TimeInterval = 0.5
    //表前视图的动画移动最大TX值
    let barOffset = screenW * 0.842
    //左边视图的动画移动最大tx值
    let leftViewOffset = screenW / 2
    //计算两个视图的移动比例
    var offsetScale: CGFloat {
        return leftViewOffset / barOffset
    }
    
    //记录第一次触摸点
    var currentP: CGPoint?
    //懒加载遮盖视图
    lazy var coverView: UIButton = {
        //创建按钮
        let btn = UIButton(frame: UIScreen.main.bounds)
        btn.backgroundColor = #colorLiteral(red: 0.501960814, green: 0.501960814, blue: 0.501960814, alpha: 1)
        //监听方法
        btn.addTarget(self, action: #selector(leftAnimation), for: UIControlEvents.touchUpInside)
        //设置透明
        btn.alpha = 0.0
        
        return btn
    }()
    ///程序启动
    override func viewDidLoad() {
          super.viewDidLoad()
        //设置UI效果
        setupUI()
        //添加手势
        let panGesture = UIPanGestureRecognizer(target: self, action: #selector(panGestureFunc(gesture:)))
        view.addGestureRecognizer(panGesture)
    }
    //设置UI
      fileprivate  func setupUI() {
      
        view.backgroundColor = UIColor.white
        //添加抽屉效果子控制器
        //左边视图
        //添加控制器到管理控制器
        addChildViewController(leftViewVc)
        //设置frame
        leftViewVc.view.frame = CGRect(x: 0, y: 0, width: barOffset - 3, height: screenH)
        //设置边界阴影
        leftViewVc.view.layer.shadowColor = UIColor.black.cgColor
        leftViewVc.view.layer.shadowOffset = CGSize(width: 3, height: 0)
        leftViewVc.view.layer.shadowOpacity = 0.9
        //添加视图
        view.addSubview(leftViewVc.view)
        
        //设置默认的形变偏移量
        leftViewVc.view.transform = CGAffineTransform(translationX: -self.leftViewOffset, y: 0)
        
        addChildViewController(tabBarVc)
        view.addSubview(tabBarVc.view)
        //获得导航左边按钮的监听事件
        tabBarVc.leftBtnBlock = {() -> () in
//            print("-----")
            //transform.tx 就是视图改变的x值
            if self.tabBarVc.view.transform.tx == 0 {
                self.rightAnimation()
            } else {
                self.leftAnimation()
            }
            
        }
    }
    //手势回调方法
   @objc fileprivate func panGestureFunc(gesture:UIPanGestureRecognizer) {
        print("*******")
    switch gesture.state {
    case .began:
        print("拖动开始")
        //先获得第一次触摸点
    currentP = gesture.location(in: gesture.view)
        
    case .changed:
  
        //先获得移动点
        let offset = gesture.translation(in: gesture.view)
        
        print("拖动中\(offset)")
        //获得新的移动点
        let newTx = tabBarVc.view.transform.tx + offset.x
        if newTx >= barOffset {
            rightAnimation()
            return
        }
        if newTx <= 0 {
            leftAnimation()
            return
        }
        //设置左边视图的移动值
        let leftNewTx = leftViewVc.view.transform.tx + offset.x * offsetScale
        
        //从新设置视图的位置
        
        tabBarVc.view.transform = CGAffineTransform(translationX: newTx, y: 0)
       leftViewVc.view.transform = CGAffineTransform(translationX: leftNewTx, y: 0)
        //恢复初始值
        gesture.setTranslation(CGPoint(), in: gesture.view)
        
        
    case .ended:
        print("拖动结束")
        //获得结束时的点
        let endP = gesture.location(in: gesture.view)
        if (currentP?.x)! < endP.x {
            rightAnimation()
        } else {
            leftAnimation()
        }
    default:
        break
    }
    }
    
    //右边滑动动画
    func rightAnimation() {
        //添加遮盖按钮
        self.tabBarVc.view.addSubview(self.coverView)
        UIView.animate(withDuration: animationTime, animations: {
         //动画移动视图
            self.tabBarVc.view.transform = CGAffineTransform(translationX: self.barOffset, y: 0)
            self.leftViewVc.view.transform = CGAffineTransform(translationX: 0, y: 0)
            //设置透明度
            self.coverView.alpha = 0.6
            
        })
    }
    //左边滑动动画
    func leftAnimation() {
        UIView.animate(withDuration: animationTime, animations: { 
            
            self.tabBarVc.view.transform = CGAffineTransform(translationX: 0, y: 0)
                self.leftViewVc.view.transform = CGAffineTransform(translationX: -self.leftViewOffset, y: 0)
            }) { (_) in
                print("动画完成")
            //取消遮盖视图
            self.coverView.removeFromSuperview()
        }
    }

}
